<template>
  <div class="loginbBody">
    <div class="loginDiv">
      <div class="login-content">
        <h1 class="login-title">用户注册</h1>
        <el-form
          :model="registerForm"
          status-icon
          label-width="100px"
          ref="registerForm"
          :rules="rules"
        >
          <el-form-item label="账号" prop="userName">
            <el-input
              style="width: 200px"
              type="text"
              v-model="registerForm.userName"
              autocomplete="off"
              size="small"
            ></el-input>
          </el-form-item>
          <el-form-item label="密码" prop="password">
            <el-input
              style="width: 200px"
              type="password"
              v-model="registerForm.password"
              show-password
              autocomplete="off"
              size="small"
            ></el-input>
          </el-form-item>
          <el-form-item label="确认密码" prop="checkPass">
            <el-input
              style="width: 200px"
              type="password"
              v-model="registerForm.checkPass"
              show-password
              autocomplete="off"
              size="small"
            ></el-input>
          </el-form-item>
          <el-form-item>
            <el-button class="butt" type="primary" :plain="true" @click="confirm()"
              >确 定</el-button
            >
          </el-form-item>
        </el-form>
        <div class="go-text" @click="goLogin">注册好了，去登录</div>
      </div>
    </div>
  </div>
</template>

<script>
// import request from "../Api/request";
import { register } from "../http/axios.js";

export default {
  name: "register",
  data() {
    // var validatePass = (rule, value, callback) => {
    //     if (value === '') {
    //       callback(new Error('请输入密码'));
    //     } else {
    //       if (this.registerForm.checkPass !== '') {
    //         this.$refs.registerForm.validateField('checkPass');
    //       }
    //       callback();
    //     }
    //   };
    // var validatePass2 = (rule, value, callback) => {
    //   if (value === "") {
    //     callback(new Error("请再次输入密码"));
    //   } else if (value !== this.registerForm.password) {
    //     callback(new Error("两次输入密码不一致!"));
    //   } else {
    //     callback();
    //   }
    // };
    return {
      registerForm: {
        userName: "",
        password: "",
        checkPass: "",
      },
      rules: {
        userName: [{ required: true, message: "请输入账号", trigger: "blur" }],
        password: [{ required: true, message: "请输入密码", trigger: "blur" }],
        checkPass: [{ required: true, message: "请再次输入密码", trigger: "blur" }],
      },
    };
  },
  methods: {
    //确认注册
    confirm() {
      this.$refs.registerForm.validate(async () => {
        console.log(this.registerForm, "22222222");
        // var userName = this.registerForm.userName
        var pass1 = this.registerForm.password;
        var pass2 = this.registerForm.checkPass;
        if (pass1 == pass2) {
          let body = {
            username: this.registerForm.userName,
            password: this.registerForm.password
          }
          register(body).then((res) => {
            console.log('---------');
            console.log(res, "注册成功");
            // alert("注册成功！去登录");
            this.$router.replace("/login");
          });
          // await request.post(`/register?userName=${this.registerForm.userName}?password${this.registerForm.password}`)
          // console.log(res);
        } else {
          alert("注册失败!!请检查输入的密码是否一致");
          return false;
        }
      });
    },
    //跳转登录
    goLogin() {
      this.$router.replace("/login");
    },
  },
};
</script>

<style scoped>
.loginbBody {
  width: 100%;
  height: 100%;
  background-color: #b3c0d1;
}
.loginDiv {
  position: absolute;
  top: 50%;
  left: 50%;
  margin-top: -200px;
  margin-left: -250px;
  width: 450px;
  height: 330px;
  background: #fff;
  border-radius: 5%;
}
.login-title {
  margin: 20px 0;
  text-align: center;
}
.login-content {
  width: 400px;
  height: 250px;
  position: absolute;
  top: 25px;
  left: 25px;
}
.butt {
  margin-left: 60px;
}
/* .element.style {
    display: flex;
    justify-content: center;
  } */
.go-text {
  text-align: center;
  font-size: 13px;
  /* margin-top: 50px; */
  cursor: pointer;
  text-decoration: underline;
}
</style>
